package logic

import (
	"context"
	"monitor/rpc/status/internal/svc"
	"monitor/rpc/status/status"
	"monitor/shared"
	"time"

	"github.com/tal-tech/go-zero/core/logx"
)

type CmdSetLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCmdSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CmdSetLogic {
	return &CmdSetLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CmdSetLogic) CmdSet(in *status.CmdSetReq) (*status.CmdSetResp, error) {
	// todo: add your logic here and delete this line
	err := l.svcCtx.Cache.SetWithExpire(shared.CacheSnPrefix+in.Sn, in.Cmd, 60*time.Duration(in.Expire)*time.Second)
	if err != nil && err == shared.ErrNotFound {
		return &status.CmdSetResp{Code: 1, Msg: "cmd set Cache error"}, nil
	}
	return &status.CmdSetResp{Code: 0, Msg: "ok"}, nil
}
